import plugin from '../../lib/plugins/plugin.js'
import fetch from 'node-fetch'
import redis from 'redis';
const redisClient = redis.createClient();

// 定义常量
const TICKET_COMMON = '通票';
const TICKET_SPECIAL = '专票';
const STAR_4 = '四星角色';
const STAR_5 = '五星角色';
const STAR_4_NORMAL = ['丹恒', '三月七', '黑塔', '阿兰', '爱斯妲', '桑博', '希露瓦', '青雀', '停云', '素裳'];
const STAR_5_NORMAL = ['姬子', '瓦尔特', '布洛妮娅', '杰帕德', '克拉拉', '彦卿', '白露'];
const STAR_3_LIGHT_CONE = '三星光锥';
const MAX_DRAW_NORMAL = 10;
const MAX_DRAW_ACTIVITY = 90;
const CHANCE_4_NORMAL = 0.1;
const CHANCE_5_ACTIVITY = 0.0001;
// 定义指令处理函数
async function handleCommand({ command, senderId }) {
      switch (command) {
              case '*领取今日抽卡道具':
                    await handleDrawProps(senderId);
                          break;
                              case '*群星跃迁':
                                    await handleDrawNormal(senderId);
                                          break;
                                              default:
                                                    break;
      }
}
// 处理领取抽卡道具指令
async function handleDrawProps(userId) {
      // 获取当前日期
        const today = new Date().toLocaleDateString();
          // 判断用户今天是否已经领取过道具
            const hasDrawn = await redisClient.hgetAsync(today, userId);
              if (hasDrawn) {
                      return '今天已经领取过道具了，请明天再来。';
              }
                // 领取道具
                  await redisClient.hsetAsync(today, userId, 'true');
                    await redisClient.hincrbyAsync(userId, TICKET_COMMON, 100);
                      await redisClient.hincrbyAsync(userId, TICKET_SPECIAL, 100);
                        return '领取100张通票和100张专票。';
}
// 处理常驻抽卡指令
async function handleDrawNormal(userId) {
      // 检查用户是否有足够的通票
        const commonTicketCount = await redisClient.hgetAsync(userId, TICKET_COMMON);
          if (!commonTicketCount || commonTicketCount < 1) {
                  return '您的通票数量不足，请先领取道具。';
          }
            // 消耗通票
              await redisClient.hincrbyAsync(userId, TICKET_COMMON, -1);
                // 抽卡
                  const drawResult = await drawNormal();
                    // 发送结果消息
                      let msg = '本次抽卡结果：';
                        if (drawResult === STAR_4) {
                                msg += '获得常驻四星角色！';
                        } else if (drawResult === STAR_5) {
                                msg += '获得常驻五星角色！';
                        } else if (drawResult === STAR_3_LIGHT_CONE) {
                                msg += '获得三星光锥。';
                        } else {
                                msg += '没有获得任何奖励。';
                        }
                          return msg;
}
// 抽常驻卡池
async function drawNormal() {
      let drawResult = '';
        let drawCount = 0;
          while (!drawResult && drawCount < MAX_DRAW_NORMAL) {
                  drawCount++;
                      if (Math.random() <= CHANCE_4_NORMAL) {
                                drawResult = STAR_4_NORMAL[Math.floor(Math.random() * STAR_4_NORMAL.length)];
                      }
          }
            if (!drawResult) {
                    drawCount = 0;
                        while (!drawResult && drawCount < MAX_DRAW_ACTIVITY) {
                                  drawCount++;
                                        if (Math.random() <= CHANCE_5_ACTIVITY) {
                                                    drawResult = STAR_5_NORMAL[Math.floor(Math.random() * STAR_5_NORMAL.length)];
                                        }
                        }
                            if (!drawResult) {
                                      drawResult = STAR_3_LIGHT_CONE;
                            }
            }
              return drawResult;
}
export default { handleCommand };
                            }
                                        }
                        }
            }
                      }
          }
}
                        }
                        }
                        }
                        }
          }
}
              }
}
      }
}